import UIKit

// ██
// ██
class Block1: Block {
    /**
     * 构造方法。
     */
    public override init() {
        super.init()
        mBlocks[1][1].mUsed = true
        mBlocks[1][2].mUsed = true
        mBlocks[2][1].mUsed = true
        mBlocks[2][2].mUsed = true
    }

    /**
     * 获取底端所在的行数。底端是指方块最下方的有效格。
     * @return 底端所在的行数
     */
    public override var bottom: Int {
        get {
            return mRow + 2
        }
    }

    /**
     * 进入预览区域。
     * @param preview   预览区域
     * @param drawer    绘图者
     */
    public override func enterPreview(_ preview: UIView, drawer: CGContext) {
        let x = Int(preview.frame.width / 2) - Const.GRID_SIZE
        let y = Int(preview.frame.height / 2) - Const.GRID_SIZE
    
        for i in 0 ..< 2 {
            for j in 0 ..< 2 {
                drawer.setFillColor(mColor)
                let rect = CGRect(x: x + Const.GRID_SIZE * j, y: y + Const.GRID_SIZE * i, width: Const.GRID_SIZE, height: Const.GRID_SIZE)
                drawer.fill(rect)
                Block.drawEdge(drawer, left: x + Const.GRID_SIZE * j, top: y + Const.GRID_SIZE * i)
            }
        }
    }

    /**
     * 擦除。
     */
    internal override func erase() {
        let rect = CGRect(x: Const.GRID_SIZE * (mLine + 1), y: (Const.GRID_SIZE * (mRow + 1)), width: Const.GRID_SIZE * 2, height: Const.GRID_SIZE * 2)
        
        super.eraseRect(rect)
    }

    /**
     * 对不能下落的处理。
     */
    internal override func handleCannotMoveDown() {
        Game.sExistentBlocks[mRow + 1][mLine + 1].mUsed = true
        Game.sExistentBlocks[mRow + 1][mLine + 2].mUsed = true
        Game.sExistentBlocks[mRow + 2][mLine + 1].mUsed = true
        Game.sExistentBlocks[mRow + 2][mLine + 2].mUsed = true
        Game.sExistentBlocks[mRow + 1][mLine + 1].mColor = mColor
        Game.sExistentBlocks[mRow + 1][mLine + 2].mColor = mColor
        Game.sExistentBlocks[mRow + 2][mLine + 1].mColor = mColor
        Game.sExistentBlocks[mRow + 2][mLine + 2].mColor = mColor
    }

    /**
     * 下落。
     * @return 成功则返回 true，否则返回 false
     */
    public override func moveDown() -> Bool {
        if mRow == 17 {
            self.handleCannotMoveDown()
            return false
        }

        if Game.sExistentBlocks[mRow + 3][mLine + 1].mUsed || 
            Game.sExistentBlocks[mRow + 3][mLine + 2].mUsed {
            self.handleCannotMoveDown()
            return false
        }

        return super.moveDown()
    }

    /**
     * 左移。
     */
    public override func moveLeft() {
        if mLine == -1 {
            return
        }
        if Game.sExistentBlocks[mRow + 1][mLine].mUsed ||
            Game.sExistentBlocks[mRow + 2][mLine].mUsed {
            return
        }

        super.moveLeft()
    }

    /**
     * 右移。
     */
    public override func moveRight() {
        if mLine == 7 {
            return
        }
        if Game.sExistentBlocks[mRow + 1][mLine + 3].mUsed ||
            Game.sExistentBlocks[mRow + 2][mLine + 3].mUsed {
            return
        }

        super.moveRight()
    }

    /**
     * 获取顶端所在的行数。顶端是指方块最上方的有效格。
     * @return 顶端所在的行数
     */
    public override var top: Int {
        get {
            return mRow + 1
        }
    }

    /**
     * 变形。
     */
    public override func transform() {
    }
}
